package com.example.wangzh.developkit.control;

import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.base.BaseActivity;
import com.example.wangzh.developkit.utils.UIUtils;
import com.example.wangzh.developkit.widget.PasswordView;

import butterknife.BindView;

/**
 * Created by wangzh on 2017/10/31.
 */

public class PasswordActivity extends BaseActivity {
    @BindView(R.id.btn_show_password_inputer)
    Button btn_show_password_inputer;
    PasswordView passwordView;
    private String firstPwd, secondPwd;

    @Override
    protected int getLayout() {
        return R.layout.activity_password;
    }

    @Override
    protected void initView() {
        initPasView();
        btn_show_password_inputer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passwordView.show();
            }
        });
    }

    private void initPasView() {
        passwordView = new PasswordView(this, new PasswordView.ResultHandler() {
            @Override
            public void handle(String time) {
                if (firstPwd == null) {
                    firstPwd = time;
                    passwordView.clearPassword();
                    passwordView.setTitle("请再次输入支付密码");
                } else {
                    secondPwd = time;
                    updatePayPassword();
                }
            }
        });
        passwordView.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                firstPwd = null;
                secondPwd = null;
                passwordView.clearPassword();
            }
        });
    }

    private void updatePayPassword() {
        if (!firstPwd.equals(secondPwd)) {
            UIUtils.showToast("两次输入密码不一致");
            passwordView.clearPassword();
            passwordView.dismissDialog();
        } else {
            UIUtils.showToast("密码输入成功，密码为：" + firstPwd);
            passwordView.dismissDialog();
        }
    }

}
